package com.example.myapplication.Penson;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.RunOnUiThreadTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.example.myapplication.ShuJu.UserPenter;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class PersonalInfoActivity extends AppCompatActivity {
    private ImageView back_button;
    private EditText nickname_edit_text;
    private RadioGroup ragroup;
    private EditText phone_number_edit_text;
    private TextView save_buttonsave_button;
    private UserPenter userPenter;
    private Gson gson;
    private RadioButton ra2;
    private RadioButton ra1;
    private String sex = "";
    private String TAG = "TAG";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iniview();
        //将信息填充
        informationfilling();
        //修改信息
        personalinformation();
    }

    private void informationfilling() {
        UserPenter.UserBean user = userPenter.getUser();
        if (user == null) {
            return;
        }
        nickname_edit_text.setText(user.getNickName());
        phone_number_edit_text.setText(user.getPhonenumber());
        if ("0".equals(user.getSex())) {
            sex ="0";
            ra1.setChecked(true);
        } else if ("1".equals(user.getSex())) {
            ra2.setChecked(true);
            sex ="1";
        }

        ragroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int checkedRadioButtonId = group.getCheckedRadioButtonId();
                if (checkedRadioButtonId == R.id.ra1) {
                    sex = "0";
                }else if(checkedRadioButtonId == R.id.ra2) {
                    sex = "1";
                }
            }
        });
    }


    private void iniview() {
        setContentView(R.layout.activity_personal_info);
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra(Commom.USERPENTER);
        Gson gson = new Gson();
        userPenter = gson.fromJson(stringExtra, UserPenter.class);


        back_button = findViewById(R.id.back_button);
        nickname_edit_text = findViewById(R.id.nickname_edit_text);
        ragroup = findViewById(R.id.ragroup);
        phone_number_edit_text = findViewById(R.id.phone_number_edit_text);
        save_buttonsave_button = findViewById(R.id.save_buttonsave_button);
        ra1 = findViewById(R.id.ra1);
        ra2 = findViewById(R.id.ra2);


    }

    //修该个人信息
    private void personalinformation() {
        save_buttonsave_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nickname_edit_text.getText().toString();
                String phone = phone_number_edit_text.getText().toString();

                JSONObject jsonObject = new JSONObject();
                if(!"".equals(name.trim())  && !"".equals(phone.trim()) && !"".equals(sex.trim())){
                    try {
                        jsonObject.put("nickName",name);
                        jsonObject.put("phonenumber",phone);
                        jsonObject.put("sex",sex);
                        sendpost(jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(PersonalInfoActivity.this,"填写的信息有误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void sendpost(String JSON) {
        OKHttpTool.doNetRequest("prod-api/api/common/user",
                OKHttpTool.PUT,
                LeavecommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(PersonalInfoActivity.this, new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                LeavecommentBean leavecommentBean  = (LeavecommentBean)obj;
                                if(leavecommentBean.getCode()==200){
                                    Toast.makeText(PersonalInfoActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },JSON);
    }
}
